import { message } from "ant-design-vue";
import axios from "axios";
import type {
    CreateAxiosDefaults,
    AxiosInstance,
    InternalAxiosRequestConfig,
    AxiosResponse,
    AxiosRequestConfig,
    AxiosRequestHeaders
} from "axios";
type ResponseDataDefault = { code: number; data: any } & Record<string, any>;
export default class HTTP<T = unknown> {
    public config: CreateAxiosDefaults<T> | undefined;
    public install: AxiosInstance;
    constructor(config: CreateAxiosDefaults<T> | undefined) {
        this.config = config;
        this.install = axios.create(this.config);
        this.init();
    }
    // 统一请求
    public request<P, R = ResponseDataDefault>(config: AxiosRequestConfig<P> = {}): Promise<AxiosResponse<R, P>> {
        return this.install(config);
    }
    // 添加拦截器
    protected init(): void {
        this.install.interceptors.request.use(
            (config: InternalAxiosRequestConfig<T>) => {
                return config;
            },
            (err) => {
                return Promise.reject(err);
            }
        );
        this.install.interceptors.response.use(
            // @ts-ignore
            (response: AxiosResponse<D, T>) => {
                if (response.status === 200) {
                    if (String(response.data.code).startsWith("2")) {
                        return Promise.resolve(response);
                    } else if (String(response.data.code).startsWith("3")) {
                        return Promise.resolve(response);
                    } else if (String(response.data.code).startsWith("4")) {
                        if (response.data.code === 403) {
                            return Promise.reject(response);
                        }
                        return Promise.reject(response);
                    } else if (String(response.data.code).startsWith("5")) {
                        return Promise.reject(response);
                    }
                }
                return Promise.reject(response);
            },
            (err) => {
                console.log("服务器错误!");
                return Promise.reject(err);
            }
        );
    }
    public post<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            method: "POST",
            data,
            headers,
            ...config
        });
    }
    public put<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "PUT",
            headers,
            ...config
        });
    }

    public delete<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "DELETE",
            headers,
            ...config
        });
    }
    public get<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        params?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            params,
            method: "GET",
            headers,
            ...config
        });
    }
}

export enum HttpMethods {
    GET = "get",
    POST = "post",
    PUT = "put",
    DELETE = "delete"
}